<template>
  <div class="dashboard-editor-container">
    <el-row style="background: #fff; padding: 16px 16px 0">
      <div class="top topSubmitBtn">
        <el-button type="primary" @click="addomainName()">添加域名</el-button>
        <p>
          指定免审计域名
        </p>
      </div>
      <div>
        <el-table stripe :data="tableData" style="width: 100%; margin: 20px 0">
          <el-table-column
            prop="date"
            label="序号"
            type="index"
            align="left"
            width="80px"
          ></el-table-column>
          <el-table-column
            prop="url"
            label="域名"
            align="left"
          ></el-table-column>
          <el-table-column label="操作" width="150px" align="left">
            <template slot-scope="scope">
              <!-- <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button> -->
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row.url)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-row>
    <el-dialog
      title="添加域名"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="dialogVisible"
      v-el-drag-dialog
      width="calc(21% - 13px)"
    >
      <div class="Dialog-title" style="margin-bottom: 10px">请输入域名:</div>
      <el-input v-model="omainName" :maxlength="500"></el-input>
      <!--底部分割线 -->
        <el-divider class="lineToFooter"></el-divider>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit()">确 定</el-button>
        <el-button type="info" @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { connrequest } from "@/api/firstPage";
import elDragDialog from "@/directive/el-drag-dialog"; // base on element-ui
export default {
  directives: { elDragDialog },
  data() {
    return {
      omainName: "",
      dialogVisible: false,
      tableData: []
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.getData();
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {},
  methods: {
    async getData() {
      this.tableData = [];
      const res = await connrequest("GetAuditFreeDomain");
      res.Data = res.Data || [];
      res.Data.map((item) => {
        this.tableData.push({ url: item });
      });
    },
    addomainName() {
      this.omainName = "";
      this.dialogVisible = true;
    },
    async submit() {
      // let isURL = /^(?=^.{3,255}$)(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*([\?&]\w+=\w*)*$/
      // if( !isURL.test(this.omainName)){
      //   this.$message.error('域名格式错误')
      //   return false
      // }
      // this.omainName = this.omainName.replace(/^https?\:\/\//i, '')
      if (!this.omainName) {
        this.$message.error("域名不能为空");
        return false;
      }
      if (
        this.tableData.find((item) => item.url === this.omainName) !== undefined
      ) {
        this.$message.error("该域名已经在免记录名单");
        return false;
      }
      let data = {
        Data: { Domain: this.omainName }
      };
      const res = await connrequest("AddAuditFreeDomain", data);
      if (res.Status.StatusCode === 0) {
        this.$message.success("域名添加成功");
        this.getData();
        this.dialogVisible = false;
      }
    },
    handleDelete(item) {
      this.$confirm("此操作将永久删除该域名, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          let data = {
            Data: { Domain: item }
          };
          const res = await connrequest("DelAuditFreeDomain", data);
          if (res.Status.StatusCode === 0) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getData();
            this.dialogVisible = false;
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>
<style scoped lang='scss'>
/* @import url(); 引入css类 */
.top {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  p {
    font-size: 15px;
    color: #409eff;
    padding-left: 20px;
  }
}

</style>